class DatasheetsController < ApplicationController
	before_filter :login_required, :except => [:index]
	
  def index
    @datasheets = Datasheet.find(:all)
  end

  def new
    @datasheet = Datasheet.new
  end

  def create
    @datasheet = Datasheet.new(params[:datasheet])
    @attachment = Attachment.create
    @attachable = params[:controller_type].singularize.capitalize.constantize.find(params[:id])
    @attachable.attachments << @attachment
    @datasheet.attachments << @attachment

    respond_to do |format|
      if @datasheet.save
        flash[:notice] = 'Datasheet was successfully uploaded.'
        format.html { redirect_to :controller => params[:controller_type], :action => :show, :id => params[:id] }
        format.xml  { head :created, :location => datasheet_url(@datasheet) }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @datasheet.errors.to_xml }
      end
    end
  end

  def destroy
    @datasheet = Datasheet.find(params[:id])
    @datasheet.destroy

    respond_to do |format|
      format.html { redirect_to datasheets_url }
      format.xml  { head :ok }
    end
  end
end
